(function(){
    "use strict";
    app.controller('swarmCtrl', ['$scope', 'httpLoad', '$rootScope','$modal','$state','$timeout','LANGUAGE',
        function($scope, httpLoad, $rootScope, $modal,$state, $timeout,LANGUAGE) {
            $rootScope.moduleTitle = '容器管理 > 集群';//定义当前页
            $rootScope.link = '/statics/css/swarm.css';//引入页面样式

            $scope.sites = [
                {site : "升序", val : "0"},
                {site : "降序", val : "1"}
            ];

            $scope.param = {
                rows: 10
            };
            $scope.isbatchDelete = true;
            //获取主机列表
            $scope.getData = function(page){
                $scope.param.page = page || $scope.param.page;
                var params = {
                        page: $scope.param.page,
                        rows: $scope.param.rows,
                        clusterName: $scope.searchByName
                    };
                httpLoad.loadData({
                    url:'/runtime/cluster/list',
                    data: params,
                    success:function(data){
                        if(data.rows&&data.rows.length!=0){
                            $scope.swarmList = data.rows;
                            $scope.totalCount = data.records;
                            $scope.isImageData = false;
                        }else{
                            $scope.isImageData = true;
                        }
                    }
                });
            };
            $scope.getData(1);

            //事件
            // $scope.paramEvent = {
            //     rows: 10,
            //     page:1
            // };
            $scope.getEvent = function(page){
                $scope.param.page = page || $scope.param.page;
                var params = {
                    page: $scope.param.page,
                    rows: $scope.param.rows,
                };
                httpLoad.loadData({
                    url:'/runtime/cluster/eventList',
                    data: params,
                    success:function(data){
                        if(data.rows&&data.rows.length!=0){
                            $scope.eventList = data.rows;
                            $scope.totalCountEvent = data.records;
                            $scope.isImageDataEvent = false;
                        }else{
                            $scope.isImageDataEvent = true;
                        }
                    }
                });
            };
            $scope.getEvent(1);
            //新增集群
            $scope.addSwarm = function(){  //打开模态
                var modalInstance = $modal.open({
                    templateUrl : '/statics/tpl/resources/swarm/addSwarm.html',  //指向上面创建的视图
                    controller : 'addSwarmModalCtrl',// 初始化模态范围
                });
                modalInstance.result.then(function(data){
                    $scope.getData();
                },function(){});
            };
            //添加
            $scope.add = function(item){  //打开模态
                var modalInstance = $modal.open({
                    templateUrl : '/statics/tpl/resources/swarm/add.html',  //指向上面创建的视图
                    controller : 'addHostSwarmModalCtrl',// 初始化模态范围
                    resolve : {
                        clusterId: function() {
                            return item.clusterId;
                        }
                    }
                });
                modalInstance.result.then(function(data){
                	$scope.getData();
                },function(){});
            };
            //移除
            $scope.remove = function(item){  //打开模态
                var modalInstance = $modal.open({
                    templateUrl : '/statics/tpl/resources/swarm/remove.html',  //指向上面创建的视图
                    controller : 'removeHostModalCtrl',// 初始化模态范围
                    resolve : {
                        itemData: function() {
                            return item;
                        }
                    }
                });
                modalInstance.result.then(function(data){
                	$scope.getData();
                },function(){});
            };
            //删除集群
            $scope.delete = function(id){  //打开模态
                var modalInstance = $modal.open({
                    templateUrl : '/statics/tpl/resources/swarm/delete.html',
                    controller : 'deleteSwarmModalCtrl',
                    resolve : {
                        id: function() {
                            return  id;
                        }
                    }
                });
                modalInstance.result.then(function(){
                    $scope.getData();
                },function(){});
            };
            //跳转详情页
            $scope.detail = function(item){
                $state.go('app.resources.hostDetail',{id:item.id,flag:1});
            };
        }
    ]);
    //新增集群ctrl
    angular.module('app').controller('addSwarmModalCtrl',['$scope','$modalInstance','LANGUAGE','httpLoad',
        function($scope,$modalInstance,LANGUAGE,httpLoad){
            $scope.haEnable = 0;
            httpLoad.loadData({
                url:'/runtime/host/unUsedHosts',
                data: {hostType: 0
                },
                success:function(data){
                    if(data.success&&data.data.length!=0){
                         $scope.types = data.data;
                         $scope.type = $scope.types[0].hostId;
                         $scope.standbys = data.data;
                         $scope.standby = $scope.standbys[0].hostId;
                    }else{
                        $scope.pop(data.message,"error");
                    }
                }
            });
            //镜像仓库下拉
            httpLoad.loadData({
                url:'/runtime/registry/listAll',
                data: {},
                success:function(data){
                    if(data.success&&data.data.length!=0){
                        $scope.registys = data.data;
                        $scope.registy = $scope.registys[0].registryId;
                    }else{
                        $scope.pop(data.message,"error");
                    }
                }
            });
            $scope.isHigh = function () {
                if($('#highId').is(':checked')){
                    $scope.haEnable = 1;
                    $("#showHigh").show();
                }else{
                    $scope.haEnable = 0;
                    $("#showHigh").hide();
                }
            }
            $scope.ok = function(){
                if($scope.type==$scope.standby){
                    $scope.pop("集群主机与备用主机一致","error");
                    return false;
                }
                if($scope.haEnable==0){
                    $scope.addData = {
                        haEnable:$scope.haEnable,
                        clusterHostId:$scope.type,
                        clusterName:$scope.clusterName,
                        serviceIp:$scope.serviceIp,
                        clusterRegistryId:$scope.registy
                    };
                }else{
                    $scope.addData = {
                        haEnable:$scope.haEnable,
                        clusterHostId:$scope.type+","+$scope.standby,
                        clusterName:$scope.clusterName,
                        serviceIp:$scope.serviceIp,
                        clusterRegistryId:$scope.registy
                    };
                }
                httpLoad.loadData({
                    url:'/runtime/cluster/create',
                    data: $scope.addData,
                    success:function(data){
                        if(data.success){
                            $scope.pop(data.message);
                            $modalInstance.close($scope.addData);
                        }else{
                            $scope.pop(data.message,"error");
                        }
                    }
                });
            };
            $scope.cancel = function(){
                $modalInstance.dismiss('cancel'); // 退出
            };
        }]);
    //添加ctrl
    angular.module('app').controller('addHostSwarmModalCtrl',['$scope','$modalInstance','clusterId','LANGUAGE','httpLoad',
        function($scope,$modalInstance,clusterId,LANGUAGE,httpLoad){
            httpLoad.loadData({
                url:'/runtime/host/unUsedHosts',
                data: {hostType: 1
                },
                success:function(data){
                    if(data.data&&data.data.length!=0){
                        var result = data.data;
                        var listall = "";
                        var lista=""
                        for(var i = 0; i < result.length; i++){
                            var obj = result[i];
                            lista = '<option value='+obj.hostId+'>'+obj.hostIp+'</option>';
                            listall+=lista;
                        }
                        $("#left_select").html(listall);
                        $scope.isImageData = false;
                    }else{
                        $scope.isImageData = true;
                    }
                }
            });
            $scope.left = function(isAll){
                var i;
                var os = new Array();
                os = $("#right_select").find("option");
                for(i=0;i<os.length;i++){
                    if(isAll){
                        var o = new Option(os[i].text,os[i].value);
                        $("#left_select").append(o);
                        $("#right_select").find("option").remove();
                    }else{
                        if(os[i].selected){
                            var o = new Option(os[i].text,os[i].value);
                            $("#left_select").append(o);
                            $("#right_select").find("option:selected").remove();
                        }
                    }
                }
            }
            $scope.right = function(isAll){
                var i,j;
                var os = new Array();
                os = $("#left_select").find("option");
                for(i=0;i<os.length;i++){
                    if(isAll){
                        var o = new Option(os[i].text,os[i].value);
                        var osright = $("#right_select").find("option");
                        for(j=0;j<osright.length;j++){
                            if(o.text==osright[j].text){
                                $("#right_select").find("option").eq(j).remove();
                            }
                        }
                        $("#right_select").append(o);
                        $("#left_select").find("option").remove();
                    }else{
                        if(os[i].selected){
                            var o = new Option(os[i].text,os[i].value);
                            var osright = $("#right_select").find("option");
                            for(j=0;j<osright.length;j++){
                                if(o.text==osright[j].text){
                                    $("#right_select").find("option").eq(j).remove();
                                }
                            }
                            $("#right_select").append(o);
                            $("#left_select").find("option:selected").remove();
                        }
                    }
                }
            }
            $scope.ok = function(){
                var id = "";
                $('#right_select option').each(function(){
                    id += $(this).val() + ",";
                });
                if(id==""){
                    $scope.pop("请选择主机");
                    return false;
                }
                httpLoad.loadData({
                    url:'/runtime/host/addToCluster',
                    data: {
                        hostId:id,
                        clusterId:clusterId
                    },
                    success:function(data){
                        if(data.success){
                            $scope.pop(data.message);
                            $modalInstance.close();
                        }else{
                            $scope.pop(data.message,"error");
                        }
                    }
                });
            };
            $scope.cancel = function(){
                $modalInstance.dismiss('cancel'); // 退出
            };
        }]);
    //移除ctrl
    angular.module('app').controller('removeHostModalCtrl',['$scope','$modalInstance','itemData','LANGUAGE','httpLoad',
        function($scope,$modalInstance,itemData,LANGUAGE,httpLoad){
            $scope.updateData = {};
            httpLoad.loadData({
                url:'/runtime/host/clusterHosts',
                data: {clusterId: itemData.clusterId
                },
                success:function(data){
                    if(data.data&&data.data.length!=0){
                        var result = data.data;
                        var listall = "";
                        var lista=""
                        for(var i = 0; i < result.length; i++){
                            var obj = result[i];
                            lista = '<option value='+obj.hostId+'>'+obj.hostIp+'</option>';
                            listall+=lista;
                        }
                        $("#left_select").html(listall);
                        $scope.isImageData = false;
                    }else{
                        $("#left_select").html("");
                        $scope.isImageData = true;
                    }
                }
            });
            $scope.left = function(isAll){
                var i;
                var os = new Array();
                os = $("#right_select").find("option");
                for(i=0;i<os.length;i++){
                    if(isAll){
                        var o = new Option(os[i].text,os[i].value);
                        $("#left_select").append(o);
                        $("#right_select").find("option").remove();
                    }else{
                        if(os[i].selected){
                            var o = new Option(os[i].text,os[i].value);
                            $("#left_select").append(o);
                            $("#right_select").find("option:selected").remove();
                        }
                    }
                }
            }
            $scope.right = function(isAll){
                var i,j;
                var os = new Array();
                os = $("#left_select").find("option");
                for(i=0;i<os.length;i++){
                    if(isAll){
                        var o = new Option(os[i].text,os[i].value);
                        var osright = $("#right_select").find("option");
                        for(j=0;j<osright.length;j++){
                            if(o.text==osright[j].text){
                                $("#right_select").find("option").eq(j).remove();
                            }
                        }
                        $("#right_select").append(o);
                        $("#left_select").find("option").remove();
                    }else{
                        if(os[i].selected){
                            var o = new Option(os[i].text,os[i].value);
                            var osright = $("#right_select").find("option");
                            for(j=0;j<osright.length;j++){
                                if(o.text==osright[j].text){
                                    $("#right_select").find("option").eq(j).remove();
                                }
                            }
                            $("#right_select").append(o);
                            $("#left_select").find("option:selected").remove();
                        }
                    }
                }
            }
            $scope.ok = function(){
                var id = "";
                $('#right_select option').each(function(){
                    id += $(this).val() + ",";
                });
                $scope.updateData.clusterId = itemData.clusterId;
                $scope.updateData.hostId = id;
                httpLoad.loadData({
                    url:'/runtime/host/removeFromCluster',
                    data: $scope.updateData,
                    success:function(data){
                        if(data.success){
                            $scope.pop(data.message);
                            $modalInstance.close($scope.updateData);
                        }else{
                            $scope.pop(data.message,"error");
                        }
                    }
                });
            };
            $scope.cancel = function(){
                $modalInstance.dismiss('cancel'); // 退出
            };
        }]);
    //删除集群ctrl
    angular.module('app').controller('deleteSwarmModalCtrl',['$scope','$modalInstance','id','LANGUAGE','httpLoad',
        function($scope,$modalInstance,id,LANGUAGE,httpLoad){ //依赖于modalInstance
            $scope.ok = function(){
                httpLoad.loadData({
                    url:'/runtime/cluster/remove',
                    data:{clusterId: id},
                    success:function(data){
                        if(data.success){
                            $scope.pop(data.message);
                            $modalInstance.close();
                        }else{
                            $scope.pop(data.message,"error");
                        }
                    }
                });
            };
            $scope.cancel = function(){
                $modalInstance.dismiss('cancel'); // 退出
            }
        }]);
})();